//
//  StretchView.m
//  <15.1>  DrawingFun
//
//  Created by chen on 16/9/18.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "StretchView.h"

@implementation StretchView

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self customInit];
        
    }
    return self;
}

- (void)awakeFromNib{
    [self customInit];
}

- (void)customInit{
    //产生随机数生成器
    srand((unsigned)time(NULL));
    
    //创建路径对象
    _path = [NSBezierPath bezierPath];
    [_path setLineWidth:3.0];
    
    NSPoint p = [self randomPoint];
    
    [_path moveToPoint:p];
    
    for (int i = 0; i <15 ; i++) {
        p = [self randomPoint];
        [_path lineToPoint:p];
    }
    
    [_path closePath];
}


- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    NSRect bounds = [self bounds];
    [[NSColor greenColor]setFill];
    [NSBezierPath fillRect:bounds];
    
    //用白色绘路径
    [[NSColor whiteColor]set];
    [_path stroke];
    
//    [_path fill];
    
}

- (NSPoint)randomPoint{
    
    NSPoint result ;
    NSRect r = [self bounds];
    
    result.x = r.origin.x + random()%(int)r.size.width ;
    result.y = r.origin.y + random()%(int)r.size.height ;
    
    return result ;
}



- (BOOL)isFlipped{
    return  YES ; 
}



@end



//更新方法 setNeedDisplay

// CGRect 是有origin和size两个成员对象的结构体。

// 与iOS不同的是坐标原点在左下角